//
//  HelpOrFeedbackViewController.swift
//  Exchange
//
//  Created by rui on 2018/7/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import ZDCChat
import ZendeskSDK

class HelpOrFeedbackViewController: BaseViewController {

    let reuseIdentifier = "HelpTableViewCell"
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .plain)
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.delegate = self
        table.dataSource = self
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.register(UINib(nibName: reuseIdentifier, bundle: nil), forCellReuseIdentifier: reuseIdentifier)
        table.tableFooterView = UIView()
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        table.rowHeight = 55
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initUI() {
        self.automaticallyAdjustsScrollViewInsets = false
        self.title = kText("帮助中心")
        self.view.addSubview(tableView)
    }
    
    func initData() {
        ZDCChatAPI.instance().addObserver(self, forConnectionEvents: #selector(timeoutAction))
    }
    
    @objc func timeoutAction() {
        let errorView = ZDCLoadingErrorView.appearance()
        let chatVc = ZDCChat.instance().chatViewController
        chatVc?.navigationController?.navigationBar.isTranslucent = false
        if ZDCChatAPI.instance().chatStatus == ZDCChatSessionStatus.connected && ZDCChatAPI.instance().connectionStatus == ZDCConnectionStatus.connected && ZDCChatAPI.instance().isAccountOnline == false {
            errorView.isHidden = true
            chatVc?.chatUI.activate()
            chatVc?.chatUI.showOfflineForm()
        }
    }
    
    deinit {
        ZDCChatAPI.instance().removeObserver(forConnectionEvents: self)
        ZDCChat.end()
    }

}

extension HelpOrFeedbackViewController: UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! HelpTableViewCell
        if indexPath.row == 0 {
            cell.title.text = kText("帮助中心")
        }else {
            cell.title.text = kText("在线咨询")
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.row == 0 {
            let viewController = ZDKHelpCenterUi.buildHelpCenterOverview()
            let nav = UINavigationController(rootViewController: viewController)
            self.present(nav, animated: true, completion: nil)
        }else {
            ZDCChat.start { (_) in
                
            }
        }
    }
}
